﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using bus = WpfDataBingding.TaskServiceReference;
using WpfDataBingding.DevTreeData;
using System.Collections.ObjectModel;

namespace WpfDataBingding.PageCollection.MonDevTaskPage
{
    /// <summary>
    /// AddDevMonWin.xaml 的交互逻辑
    /// </summary>
    public partial class AddDevMonWin : Window
    {
        
        UserControl owner_UC = null;//窗口所属的主控件

        /// <summary>
        /// 窗口所属的用户控件
        /// </summary>
        public UserControl Owner_UC
        {
            get { return owner_UC; }
            set { owner_UC = value; }
        }
        
        //UserControl_DevMonConfig userControl_DevMonConfig;
        public AddDevMonWin()
        {
            InitializeComponent();
            this.Closed += new EventHandler(AddDevMonWin_Closed);//添加关闭处理
            DevMonConfig_Grid.DataContext = this.DataContext;
        }

        /// <summary>
        /// 以grid的DataContext为数据源
        /// </summary>
        new public Object DataContext
        {
            get { return DevMonConfig_Grid.DataContext; }
            set { DevMonConfig_Grid.DataContext = value; }
        }

        /// <summary>
        /// 确定添加设备监控
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取所选设备
                Device dc = this.selectDev_comboBox.SelectedItem as Device;
                if (dc == null)
                    throw new ApplicationException("未选择要监视的设备");
                //将选择的设备调用远程服务添加监控

                try
                {
                    int time = 1000;////////////////////////debug
                    MonTaskConfigration.addMonDevTask(dc, time);
                }
                catch (Exception exp)
                {
                    MessageBox.Show("添加对设备" + dc.DevID + "的监控操作失败：" + exp.Message);
                    return;
                }
                //销毁对话窗体...手动释放资源？
                //..
                this.Close();
                //获取母窗口    
                MonDevTask_UC winDady = (this.Owner_UC as MonDevTask_UC);            
                //添加图标
                winDady.AddDevIconToDropPanel(dc);
                //设父窗口可用            
                //winDady.IsEnabled = true;
                //bool aa = winDady.Focus();
            }
            catch (Exception exp)
            {
                MessageBox.Show("出错："+exp.Message);
            }

        }

        /// <summary>
        /// 窗体关闭事件触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void AddDevMonWin_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.Owner_UC.IsEnabled = true;
            this.Owner_UC.Focus();
        }

        /// <summary>
        /// 取消设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            //设父窗口可用            
            //this.Owner_UC.IsEnabled = true;
            //this.Owner_UC.Focus();
        }

    }
}
